package appapi.mapper;

import appapi.Request.AlarmPageReq;
import appapi.Request.AlarmStatisticsPageReq;
import appapi.Vo.brider.AlarmVO;
import appapi.entity.bridge.vo.AlarmVo;
import appapi.entity.bridge.DeviceAlarm;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;


/**
 * @author hlm
 * @date
 * @description: 设备告警记录
 */
@Mapper
public interface AlarmMapper extends BaseMapper<DeviceAlarm> {

    List<AlarmVO> getAlarmByPage(@Param("req") AlarmPageReq req);

    Long selectIdByDetail(@Param("entity") DeviceAlarm entity);
    Integer autoRestoreAlarm(@Param("entity") DeviceAlarm entity);

    List<DeviceAlarm> selectByType(@Param("deviceType") Integer deviceType , @Param("orgaId") Integer orgaId);

    @Select("SELECT id.device_type AS deviecNO, COUNT(id.device_type) AS alarmCount, da.alarm_status AS alarmStatus " +
            "FROM device_alarm da " +
            "LEFT JOIN iot_device id ON id.device_sn = da.device_sn " +
            "LEFT JOIN project p ON da.proj_id = p.proj_id " +
            "LEFT JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE id.device_sn = #{deviceSn} " +
            "AND org.orga_id = #{orgaId} " +
            "GROUP BY id.device_type, da.alarm_status")
    List<AlarmVo> selectByTypes(@Param("deviceSn") String deviceSn, @Param("orgaId") Integer orgaId);


    List<DeviceAlarm> selectDeviceAlarm(@Param("req") AlarmStatisticsPageReq req);

    int selecDeviceAlarmtCount(@Param("orgaId") Integer orgaId);

    List<DeviceAlarm> selectAlarmtCountList(@Param("orgaId") Integer orgaId);

    List<DeviceAlarm> selectListByAlarmLevel(@Param("orgaId") Integer orgaId);

    int selectCountByTime(@Param("startTime") Date startTime , @Param("endTime") Date endTime , @Param("orgaId") Integer orgaId);

    List<DeviceAlarm> selectListByTime(@Param("startTime") Date startTime , @Param("endTime") Date endTime , @Param("orgaId") Integer orgaId);

    @Select("SELECT COUNT(*) FROM device_alarm da " +
            "JOIN project p ON da.proj_id = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE org.orga_id = #{orgaId}")
    int countByOrgId(@Param("orgaId") Integer orgaId);


    @Select("SELECT COUNT(*) FROM device_alarm da " +
            "JOIN project p ON da.proj_id = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE org.orga_id = #{orgaId} AND da.alarm_status = 0")
    int countUntreatedByOrgId(@Param("orgaId") Integer orgaId);

    @Select("SELECT COUNT(*) FROM device_alarm da " +
            "JOIN project p ON da.proj_id = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE org.orga_id = #{orgaId} AND da.alarm_status = 1")
    int countDisposedByOrgId(@Param("orgaId") Integer orgaId);


    @Select("SELECT COUNT(*) FROM device_alarm da " +
            "JOIN project p ON da.proj_id = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE org.orga_id = #{orgaId} AND da.alarm_status = 1 AND da.device_sn = #{deviceSn}")
    int countDisposedByOrgIdAndDeviceSn(@Param("orgaId") Integer orgaId, @Param("deviceSn") String deviceSn);


    @Select("SELECT COUNT(*) FROM device_alarm da " +
            "JOIN project p ON da.proj_id = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE org.orga_id = #{orgaId} AND da.alarm_status = 0 AND da.device_sn = #{deviceSn}")
    int countDisposedByOrgIdAndDeviceSnS(@Param("orgaId") Integer orgaId, @Param("deviceSn") String deviceSn);

    @Select("SELECT * FROM device_alarm da " +
            "JOIN project p ON da.proj_id = p.proj_id " +
            "JOIN organization org ON p.orga_id = org.orga_id " +
            "WHERE org.orga_id = #{orgaId}")
    List<DeviceAlarm> listByOrgId(@Param("orgaId") Integer orgaId);

    Integer selectSceneDeviceAlarmCount(@Param("sceneId") Long sceneId);

    Integer selectSceneDeviceUnHandleAlarmCount(@Param("sceneId") Long sceneId);

    List<AlarmVo> selectSceneAlarmByDeviceType(@Param("sceneId") Long sceneId);
}
